package cn.le.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 项目名  jdrx-bigdata-metadata
 * Created by zhongdev.
 * Created at 2018/1/23
 * 描述:下划线与驼峰转换
 */
public class UnderLineToCamel {
    public static String underlineToCamel(String line){
        if(line==null||"".equals(line)){
            return "";
        }
        StringBuffer sb=new StringBuffer();
        Pattern pattern=Pattern.compile("_(\\w)");
        Matcher matcher=pattern.matcher(line);
        while(matcher.find()){
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String camelToUnderline(String line){
        if(line==null||"".equals(line)){
            return "";
        }
        //line=String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
        StringBuffer sb=new StringBuffer();
        Pattern pattern=Pattern.compile("[A-Z]");
        Matcher matcher=pattern.matcher(line);
        while(matcher.find()){
            matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString().toLowerCase();
    }
}
